package com.mtz.config;

import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

import static springfox.documentation.spi.DocumentationType.SWAGGER_2;

/**
 * @author 煜赜
 * @date 2018/12/5 0005 上午 8:51
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket newsApi() {
        //在配置好的配置类中增加此段代码即可
        ParameterBuilder ticketPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        //name表示名称，description表示描述
        ticketPar.name("token").description("登录校验")
                .modelRef(new ModelRef("string")).parameterType("header")
                //required表示是否必填，defaultvalue表示默认值
                .required(false).defaultValue("token").build();

        //添加完此处一定要把下边的带***的也加上否则不生效
        pars.add(ticketPar.build());

        Docket docket = new Docket(SWAGGER_2);
        docket.enable(true);
        docket.apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .paths(PathSelectors.regex("/.*"))
                .build()
                //************把消息头添加
                .globalOperationParameters(pars);
        return docket;
    }

    private ApiInfo apiInfo(){
        //设置接口文档的标题
        return new ApiInfoBuilder().title("iSchool萌团子接口测试平台")
                .description("iSchool萌团子 项目web api接口规范：")
                //创建人
                .contact(new Contact("煜赜", "", ""))
                .termsOfServiceUrl("http://www-03.ibm.com/software/sla/sladb.nsf/sla/bm?Open")
                //版本号，点击进去是项目的地址
                .license("iSchool萌团子 Version 1.0")
                .licenseUrl("#")
                .version("1.0")
                .build();
    }
}

